var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    is_start: false,
    is_move: false,
    active:0,
    searSuperName:'',
    typename: ['合作中', '历史合作']
  },
  // 切换状态
  myselect:function(e){
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 0
    })
    this.setData({
      active: e.currentTarget.dataset.id
    });
    this.updatelist();
  },
  // 添加供应商
  add_super:function(){
    wx.navigateTo({
      url: '/pages/wo/gongyingshangguanli/addgongyingshang/addgongyingshang'
    })
  },
  // 前往对账
  toduizhang_zi:function(){
    var val={};
    var loginInfo=this.data.loginInfo;
    val.logo=loginInfo.logo && loginInfo.logo!=0?loginInfo.logo:'http://www.xiaobangcai.com/upload/smallprogram/super_def.png';
    val.company_name=loginInfo.client_abbre;
    val.real_name=loginInfo.real_name!= null?loginInfo.real_name:loginInfo.nickname;
    console.log(loginInfo);
    val.phone=loginInfo.phone;
    val.status=1; 
    wx.navigateTo({
      url: '/super_space/pages/supper/supperguanli/supperguanli?id='+'-10010'+"&val="+JSON.stringify(val)+'&is_zi_cai='+1
    });  
  },
  // 去供应商管理页面
  itemClick:function(e){
    var status=e.currentTarget.dataset.val.status;
    var supplier_id = e.currentTarget.dataset.val.id;
    console.log(e.currentTarget.dataset.val);
    var val= e.currentTarget.dataset.val;
    if (status >= 0){
      wx.navigateTo({
        url: 'supperguanli/supperguanli?id='+ supplier_id+"&val="+JSON.stringify(val)+'&is_zi_cai='+0
      })
    } else if (status == -1){
      wx.navigateTo({
        url: '/pages/wo/gongyingshangguanli/gongyingshangdetail/gongyingshangdetail?id=' + supplier_id
      })
    } 
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      loginInfo: app.globalData.loginInfo
    });
  },
  updatelist:function(){
    var that = this;
    app.AppHttp('api/Client/SupplierList', {
      openid: app.globalData.openid,
      type: 1
    }, function (res) {
      if(res.data.status==-1){
        that.setData({
          rightDatas:[]
        });
        return;
      };
      var active_have=-1;
      if(that.data.active==0){
        active_have = res.data.data[0].findIndex(function(item) {
          return (item.status!=0);
        });
      }else if(that.data.active==1){
        var active_have = res.data.data[0].findIndex(function(item) {
          return (item.status==0);
        });
      };
      var rightDatas=[];
      var loginInfo=that.data.loginInfo;
      if(loginInfo.hq_is_store==1){
        let logo;
        if(loginInfo.brand_logo){
          if(loginInfo.brand_logo.substring(0,7)=='/upload'){
            logo='http://www.xiaobangcai.com/'+loginInfo.brand_logo
          }else{
            logo=loginInfo.brand_logo
          };
        }else{
          if(loginInfo.supplier_logo.substring(0,7)=='/upload'){
            logo='http://www.xiaobangcai.com/'+loginInfo.supplier_logo
          }else{
            logo=loginInfo.supplier_logo
          };
        }
        rightDatas[0]={
          id:loginInfo.group_id,
          logo:logo,
          supplier_id:loginInfo.group_id,
          business_id:loginInfo.group_id,
          status:1,
          company_name:loginInfo.group_name,
          supplier_type:3,
          is_zicai_supplier:0
        }    
        rightDatas=rightDatas.concat(res.data.data[0])
        console.log(rightDatas)
      }else{
        rightDatas=res.data.data[0];
      };
      rightDatas = rightDatas.filter(function (item) {   
        return item.company_name  
      });
      rightDatas = rightDatas.map(function (item) {   
        if(item.company_name.indexOf(that.data.searSuperName)!=-1){
          item.isSear=true
        };
        return item  
      });
      that.setData({
        rightDatas: rightDatas,
        active_have:active_have  //当前状态的供应商数量
      });
    })
  },
  // 搜索供应商名称
  searSuperInput:function(e){
    this.setData({
      searSuperName:e.detail.value
    });
    this.updatelist();
  },
  searSuper:function(){
    this.updatelist();
  },
  searSuperConf:function(){
    this.updatelist();
  },
  delsuper:function(){
    this.setData({
      searSuperName:''
    });
    this.updatelist();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.updatelist();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.updatelist();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  }
})